package cn.sdormitory.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.sdormitory.basedata.dao.BDormitoryDao;
import cn.sdormitory.basedata.entity.BDormitory;
import cn.sdormitory.common.constant.CommonConstant;
import cn.sdormitory.service.AppHygieneService;
import cn.sdormitory.service.AppSdHygieneItemRecordService;
import cn.sdormitory.smartdor.dao.HygieneDeductDao;
import cn.sdormitory.smartdor.dao.SdHygieneDao;
import cn.sdormitory.smartdor.dao.SdHygieneItemRecordDao;
import cn.sdormitory.smartdor.entity.HygieneDeduct;
import cn.sdormitory.smartdor.entity.SdHygiene;
import cn.sdormitory.smartdor.entity.SdHygieneItemRecord;
import cn.sdormitory.smartdor.entity.SdLeave;
import cn.sdormitory.smartdor.vo.SdHygieneVo;
import cn.sdormitory.sysset.dao.SyssetHygieneDeductDao;
import cn.sdormitory.sysset.entity.SyssetHygieneDeduct;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @创建人：zhouyang
 * @创建时间：2021/1/8 15:10
 * @version：V1.0
 */
@Service
public class AppHygieneServiceImpl extends ServiceImpl<SdHygieneDao, SdHygiene> implements AppHygieneService{
    @Resource
    private SdHygieneDao sdHygieneDao;
    @Resource
    private BDormitoryDao bDormitoryDao;
    @Resource
    private SyssetHygieneDeductDao syssetHygieneDeductDao;
    @Resource
    private HygieneDeductDao hygieneDeductDao;

    @Autowired
    SdHygieneItemRecordDao itemRecordDao;

    @Override
    public List<SdHygieneVo> getHygieneAll(String checkDate, String storey,String buildingNo) {
        return sdHygieneDao.getAppHygieneAll(checkDate,storey,buildingNo);
    }

    @Override
    public List<BDormitory> getBDormitoryListByCol(String buildingNo, String storey) {
        return bDormitoryDao.getBDormitoryListByCol(buildingNo,storey);
    }

    @Override
    public List<SyssetHygieneDeduct> getHyDeListAll() {
        return syssetHygieneDeductDao.selectList(new LambdaQueryWrapper<SyssetHygieneDeduct>().eq(SyssetHygieneDeduct::getStatus, CommonConstant.VALID_STATUS));
    }

    @Override
    public SdHygiene getByCheckDateAndBdId(String checkDate, Long bdormitoryId) {
        LambdaQueryWrapper<SdHygiene> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SdHygiene::getCheckDate, checkDate);
        wrapper.eq(SdHygiene::getBdormitoryId, bdormitoryId);
        return sdHygieneDao.selectOne(wrapper);
    }

    @Override
    public BDormitory getBDormitoryById(Long id) {
        return bDormitoryDao.selectById(id);
    }

    @Override
    public SyssetHygieneDeduct getSyssetHygieneDeductById(Long id) {
        return syssetHygieneDeductDao.selectById(id);
    }

    @Override
    public int insertSdHygieneAndDeduct(SdHygiene sdHygiene) {
        int result = this.baseMapper.insert(sdHygiene);
        //保存卫生与卫生项关系
        hygieneDeductDao.insertHygieneAndHygieneDeduct(sdHygiene.getId(),sdHygiene.getDeductIds());
        return result;
    }

    @Override
    public int create(SdHygiene sdHygiene) {
        return this.baseMapper.insert(sdHygiene);
    }

    @Override
    public String getDeOptionsById(Long hygieneId) {
        return sdHygieneDao.getDeOptionsById(hygieneId);
    }

    @Override
    public SdHygiene getAppHygieneById(Long id) {
        return sdHygieneDao.getAppHygieneById(id);
    }

    @Override
    public int delete(Long id) {
        return sdHygieneDao.deleteById(id);
    }

    @Override
    public int updateSdHygieneAndDeduct(SdHygiene sdHygiene) {
        //先删除卫生与卫生项关系
        delAndCreateHygiene(sdHygiene.getId(), sdHygiene.getDeductIds());
        // 更新卫生信息
        return sdHygieneDao.updateById(sdHygiene);
    }

    @Override
    public void delAndCreateHygiene(Long hygieneId, List<Long> deductIdList) {
        hygieneDeductDao.delete(new LambdaQueryWrapper<HygieneDeduct>().eq(HygieneDeduct::getHygieneId, hygieneId));
        if (CollUtil.isNotEmpty(deductIdList)) {
            //保存卫生与卫生项关系
            insertHygieneAndHygieneDeduct(hygieneId, deductIdList);
        }
    }

    @Override
    public void insertHygieneAndHygieneDeduct(Long hygieneId, List<Long> deductIdList) {
        hygieneDeductDao.insertHygieneAndHygieneDeduct(hygieneId, deductIdList);
    }

    @Override
    public List<Long> listDeductIdByHygieneId(Long hygieneId) {
        List<Long> ids = new ArrayList<>();
        List<HygieneDeduct> hygieneDeducts = hygieneDeductDao.selectList(new LambdaQueryWrapper<HygieneDeduct>().eq(HygieneDeduct::getHygieneId, hygieneId));
        if (CollectionUtil.isNotEmpty(hygieneDeducts)) {
            hygieneDeducts.forEach(item -> {
                ids.add(item.getDeductId());
            });
            return ids;
        }
        return null;
    }

    @Override
    public SdHygieneVo getAppHygieneByBIdAndDate(Long bdormitoryId, String checkDate) {

        SdHygieneVo sdHygieneVo = sdHygieneDao.getAppHygieneByBIdAndDate(bdormitoryId, checkDate);
        if(sdHygieneVo != null){
            LambdaQueryWrapper<SdHygieneItemRecord> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SdHygieneItemRecord::getBdormitoryId,bdormitoryId)
                    .eq(SdHygieneItemRecord::getCheckDate,checkDate);
            List<SdHygieneItemRecord> sdHygieneItemRecords = itemRecordDao.selectList(wrapper);
            sdHygieneVo.setHygieneItemRecordList(sdHygieneItemRecords);
        }
        return sdHygieneVo;
//        return sdHygieneDao.getAppHygieneByBIdAndDate(bdormitoryId,checkDate);
    }
}
